package cn.whale.killorder.web.controller;

import cn.whale.basic.utils.JSONResult;
import cn.whale.killorder.domain.KillOrder;
import cn.whale.killorder.service.IKillOrderService;
import cn.whale.pay.dto.AlipayNotifyDto;
import cn.whale.pay.dto.AlipayReturnDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class PayController {

    @Autowired
    private IKillOrderService killOrderService;

    @GetMapping("/alipay/return")
    public JSONResult alipayReturn(AlipayReturnDto alipayReturnDto)  {
        KillOrder killOrder = killOrderService.selectByOrderNo(alipayReturnDto.getOut_trade_no());
        if(killOrder != null && killOrder.getTotalPrice().compareTo(alipayReturnDto.getTotal_amount()) == 0){
            String statusName = killOrder.getStatusPay().intValue() == KillOrder.STATUS_PAY_OK ?"成功":"失败";
            String message = String.format("支付 %s，支付金额 %s ",statusName, alipayReturnDto.getTotal_amount());
            return JSONResult.success(message);
        }
        return JSONResult.error("参数错误[total_amount]");
    }

}
